Skip to content

Report regexes with conflicting inline flags as invalid, not an error - #1563

Closed
yzxcj797 wants to merge 1 commit into
python-jsonschema:mainfrom
yzxcj797:fix/regex-format-conflicting-flags-1558
Closed

yzxcj797 wants to merge 1 commit into
python-jsonschema:mainfrom
yzxcj797:fix/regex-format-conflicting-flags-1558

Conversation

@yzxcj797

Copy link
Copy Markdown

Fixes #1558.

Root cause

re.compile raises ValueError — which is not an re.error subclass — when a pattern sets incompatible inline flags in separate groups ("(?u)(?a)" or "(?a)(?u)"). The regex format checker declared raises=re.error only, so the exception escaped FormatChecker.check and crashed the caller instead of reporting the string as an invalid regex:

fc.conforms("[unterminated", "regex")  # False — re.error, caught
fc.conforms("(?u)(?a)", "regex")       # ValueError: ASCII and UNICODE flags are incompatible

The single-group form ("(?ua)") fails compilation with re.error and was already reported correctly; only the two-group form escaped.

Fix

Declare raises=(re.error, ValueError) on is_regex, so the conflicting-flags ValueError becomes a FormatError with the original exception as its cause, exactly like any other invalid regex.

The related-but-distinct exception types from #1526 (OverflowError on an oversized repeat) and #1538 (RecursionError on deep nesting) are left to their own issues — this PR only widens the declaration for the ValueError family demonstrated in #1558.

Test

test_regex_conflicting_inline_flags_report_invalid_not_error in jsonschema/tests/test_format.py: both flag orders raise FormatError / conform False, the single-group form and a plain re.error case still report invalid, and a valid pattern still passes.

Differential: the new test errors with the escaped ValueError on main and passes with the change. Full suite: python -m unittest discover jsonschema.tests — 8455 tests, only a pre-existing local test_exceptions import error unrelated to this change (also present on stashed main).

re.compile raises ValueError (not an re.error subclass) when a pattern
sets incompatible inline flags in separate groups, e.g. '(?u)(?a)'. The
regex format checker declared raises=re.error only, so the ValueError
escaped FormatChecker.check and crashed the caller instead of reporting
the string as an invalid regex (python-jsonschema#1558).

Declare ValueError alongside re.error. The single-group form
(e.g. '(?ua)') already failed compilation with re.error and was
reported correctly; only the two-group form escaped.
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 python-jsonschema | 🛠️ Build #34176377 | 📁 Comparing f2a9514 against latest (0abde23)

  🔍 Preview build  

1 file changed
± _modules/jsonschema/_format/index.html

This branch was previously deployed

1 inactive deployment
PyPI — f2a95140 Deployed Aug 21, 2026 by yzxcj797 via Build and publish #4526
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

regex format checker crashes with ValueError on conflicting inline flags

2 participants